package com.atguigu.eduservice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.frontVo.CourseQueryVo;
import com.atguigu.eduservice.entity.vo.frontVo.CourseWebVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ZhanK
 * @since 2022-02-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //课程描mapper述注入
    @Autowired
    EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    EduChapterMapper eduChapterMapper;
    @Autowired
    EduVideoMapper eduVideoMapper;

    //添加课程信息的基本方法
    @Override
    public String saveCourseInfoVo(CourseInfoVo courseInfoVo) {
        //向课程表添加基本信息
        if (courseInfoVo != null) {
            EduCourse eduCourse = new EduCourse();
            BeanUtils.copyProperties(courseInfoVo, eduCourse);
            int insert = baseMapper.insert(eduCourse);
            if (insert == 0) { //返回是0就受影响行为0 ，没插入数据库表
                throw new GuliException(20001, "添加课程信息失败");
            }
            //向课程简介添加基本信息
            EduCourseDescription eduCourseDescription = new EduCourseDescription();
            eduCourseDescription.setDescription(courseInfoVo.getDescription());
            eduCourseDescription.setId(eduCourse.getId());
            eduCourseDescriptionService.save(eduCourseDescription);
            return eduCourse.getId();
        }
        return null;
    }

    //根据课程id查询课程的基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        //1查询课程表类容
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //封装到CourseInfoVo中
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        //2查询描述表
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        //封装到CourseInfoVo中
        if (description != null) {
            courseInfoVo.setDescription(description.getDescription());
        }

        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0) {

            throw new GuliException(20001, "修改课程信息失败");

        }

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);
        boolean b = eduCourseDescriptionService.updateById(eduCourseDescription);

    }

    //查询最终发布的课程信息，里面包含了多张表的联合查询
    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }
    //课程发布后的列表分页查询，根据传入查询对象的属性进行判断，进行多条件联合查询

    @Override
    public Page<CoursePublishVo> getPageCourse(Page<CoursePublishVo> page, CoursePublishVo courseVo) {

        return page.setRecords(this.baseMapper.getPageCourse(page, courseVo));
    }

    @Override
    public int deleteCourseById(String id) {
        //根据课程ID删除章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);
        int delete = eduChapterMapper.delete(wrapper); //成功返回1
        //根据课程id删除小节
        QueryWrapper<EduVideo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", id);
        int delete1 = eduVideoMapper.delete(wrapper1);
        int i = baseMapper.deleteById(id);

        return i;
    }

    //根据前端传入对象属性的值进行查询，然后导出Excel
    @Override
    public List<CoursePublishVo> getPageCourseExcel(CoursePublishVo coursePublishVo) {

        List<CoursePublishVo> list = baseMapper.getPageCourseExcel(coursePublishVo);
        return list;
    }

    @Override
    public Map<String, Object> getTeacherInfo(Page<EduCourse> queryVoPage, CourseQueryVo courseQueryVo) {

        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        //判断条件是否为空
        if (!StrUtil.hasEmpty(courseQueryVo.getSubjectParentId())){//一级分类
            queryWrapper.eq("subject_parent_id",courseQueryVo.getSubjectParentId());
        }
        if (!StrUtil.hasEmpty(courseQueryVo.getSubjectId())){//二级分类
            queryWrapper.eq("subject_id",courseQueryVo.getSubjectId());
        }
        if (!StrUtil.hasEmpty(courseQueryVo.getBuyCountSort())) {//销量排序
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StrUtil.hasEmpty(courseQueryVo.getGmtCreateSort())) {//时间排序
            queryWrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseQueryVo.getPriceSort())) {//价格排序
            queryWrapper.orderByDesc("price");
        }
        //封装到page里面
        baseMapper.selectPage(queryVoPage, queryWrapper);
        long total = queryVoPage.getTotal();
        List<EduCourse> records = queryVoPage.getRecords();
        long current = queryVoPage.getCurrent();
        long size = queryVoPage.getSize();
        boolean hasNext = queryVoPage.hasNext();
        boolean hasPrevious = queryVoPage.hasPrevious();
        long pages = queryVoPage.getPages();

        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;

    }
//    Front前端页面查询出课程的详细信息，包含讲师、课程分类、课程基本信息、课程简介多表查询
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }


}
